﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Public Class frmNghiemThu

#Region "proc"

    Private Sub InitLookup_ThiCong()
        Me.lueSoTc.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("sotc", "Số hồ sơ", 30, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("_ngaylap", "Ngày lập", 40, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center)})
        Me.lueSoTc.Properties.DisplayMember = "sotc"
        Me.lueSoTc.Properties.ValueMember = "sotc"

        Dim myTcF As New ThiCongF
        Dim myDtb As New DataTable
        myDtb = myTcF.GetMasterAll
        Me.lueSoTc.Properties.DataSource = myDtb
        Me.lueSoTc.EditValue = ""
    End Sub

    Private Sub InitTree()

        With Me.TreeList1
            .OptionsBehavior.Editable = False
            .OptionsView.ShowColumns = False
            .OptionsView.ShowHorzLines = False
            .OptionsView.ShowVertLines = False
            .OptionsView.ShowIndicator = False

            Me.TreeListColumn1.Visible = False
            Me.TreeListColumn2.Width = 250
            Me.TreeListColumn3.Visible = False
        End With


    End Sub

    Private Sub LoadTree()

        With Me.TreeList1
            .OptionsBehavior.Editable = False
            .OptionsView.ShowColumns = False
            .OptionsView.ShowHorzLines = False
            .OptionsView.ShowVertLines = False
            .OptionsView.ShowIndicator = False

            Me.TreeListColumn1.Visible = False
            Me.TreeListColumn2.Width = 250
            Me.TreeListColumn3.Visible = False

            Dim Node As TreeListNode = Nothing
            Dim Node0 As TreeListNode = Nothing
            Dim Node1 As TreeListNode = Nothing

            .Nodes.Clear()

            Dim myNtF As New NghiemThuF
            Dim myDtb As New DataTable
            Dim i As Integer

            'all
            Node0 = Me.TreeList1.AppendNode(New Object() {0, "Danh sách chiết tính", -1}, Node)

            'trangthai
            myDtb = myNtF.GetListTrangThai(Me.lueSoTc.EditValue)
            For i = 0 To myDtb.Rows.Count - 1
                Node1 = .AppendNode(New Object() {1, myDtb.Rows(i).Item("diengiai"), myDtb.Rows(i).Item("trangthai")}, Node0)
            Next

            .ExpandAll()
        End With


    End Sub

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "sophieu"
            .Columns(i).Caption = "Số phiếu"
            .Columns(i).Width = 35

            i = 1
            .Columns(i).FieldName = "so_don"
            .Columns(i).Caption = "Số đơn"
            .Columns(i).Width = 50

            i = 2
            .Columns(i).FieldName = "ngaylap"
            .Columns(i).Caption = "Ngày lập"
            .Columns(i).Width = 40

            i = 3
            .Columns(i).FieldName = "khachhang"
            .Columns(i).Caption = "Khách hàng"
            .Columns(i).Width = 60

            i = 4
            .Columns(i).FieldName = "diachi"
            .Columns(i).Caption = "Địa chỉ"
            .Columns(i).Width = 140

            i = 5
            .Columns(i).FieldName = "trangthai"
            .Columns(i).Visible = False
        End With


    End Sub

    Private Sub LoadGrid()
        If Me.lueSoTc.EditValue Is Nothing Then Exit Sub
        If Me.TreeList1.FocusedNode Is Nothing Then Exit Sub

        Dim myNtF As New NghiemThuF
        Dim myDtb As New DataTable
        myDtb = myNtF.GetListChietTinh(Me.lueSoTc.EditValue, Me.TreeList1.FocusedNode.Item(2))
        Me.GridControl1.DataSource = myDtb

        Me.GridView1.ExpandAllGroups()
    End Sub

#End Region

    Private Sub frmNghiemThu_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmNghiemThu_Exists = False
    End Sub

    Private Sub frmNghiemThu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitLookup_ThiCong()
        InitTree()
        InitGrid()
    End Sub

    Private Sub TreeList1_FocusedNodeChanged(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.FocusedNodeChangedEventArgs) Handles TreeList1.FocusedNodeChanged
        LoadGrid()

        'trang thai
        If Me.TreeList1.FocusedNode Is Nothing Then
            Me.btnCapNhatDanhSach.Enabled = False
            Exit Sub
        End If

        If Me.TreeList1.FocusedNode.Item(2) = 2 Or Me.TreeList1.FocusedNode.Item(2) = 3 Then
            Me.btnCapNhatDanhSach.Enabled = True
        Else
            Me.btnCapNhatDanhSach.Enabled = False
        End If


    End Sub

    Private Sub lueSoTc_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lueSoTc.EditValueChanged
        If Me.lueSoTc.EditValue <> "" Then
            LoadTree()
        End If

    End Sub

    Private Sub GridView1_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged
        If Me.GridView1.GetFocusedRowCellValue("trangthai") = 0 Then
            Me.btnNhapNghiemThu.Enabled = True
        Else
            Me.btnNhapNghiemThu.Enabled = False
        End If
    End Sub

    Private Sub NhapNghiemThu()
        If frmNghiemThu_CapNhat_Exists Then Exit Sub
        frmNghiemThu_CapNhat_Exists = True

        Dim f As New frmNghiemThu_CapNhat
        f.txtSoTc.EditValue = Me.lueSoTc.EditValue
        f.txtSoPhieu.EditValue = Me.GridView1.GetFocusedRowCellValue("sophieu")
        f.txtMaLoaiVatTu.EditValue = "D"
        f.MdiParent = _frmMenu
        f.Show()
    End Sub

    Private Sub GridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        If Me.GridView1.GetFocusedRowCellValue("trangthai") = 0 Then
            NhapNghiemThu()
        End If
    End Sub

    Private Sub btnNhapNghiemThu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapNghiemThu.Click
        NhapNghiemThu()
    End Sub

    Private Sub btnCapNhatDanhSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhatDanhSach.Click
        Dim f As New frmNghiemThu_TrangThai
        f.txtSoTc.EditValue = Me.lueSoTc.EditValue
        f.txtTrangThai.EditValue = Me.TreeList1.FocusedNode.Item(2)
        f.ShowDialog()

        LoadTree()
    End Sub

End Class